using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    [Serializable]
    public class DACheques
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DAChequesInsert
        /// <summary>
        /// Inserta un Cheque en la table Cheques
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cheques"></param>
        public void DAChequesInsert(string Connection, Cheques cheque)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Cheques_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Ventas", DbType.Int64, cheque.Id_Ventas);
                    DataBase.AddInParameter(cmd, "@Item", DbType.Int16, cheque.Item);
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, cheque.Sucursal);
                    DataBase.AddInParameter(cmd, "@CodBanco", DbType.Int16, cheque.CodBanco);
                    DataBase.AddInParameter(cmd, "@Banco", DbType.String, cheque.Banco);
                    DataBase.AddInParameter(cmd, "@Numero", DbType.String, cheque.Numero);
                    DataBase.AddInParameter(cmd, "@FechaCh", DbType.DateTime, cheque.FechaCh);
                    DataBase.AddInParameter(cmd, "@Monto", DbType.Decimal, cheque.Monto);
                    DataBase.AddInParameter(cmd, "@IdEstado", DbType.Int16, cheque.IdEstado);
                    DataBase.AddInParameter(cmd, "@Estado", DbType.String, cheque.Estado);
                    DataBase.AddInParameter(cmd, "@FechaEstado", DbType.DateTime, cheque.FechaEstado);
                    DataBase.AddInParameter(cmd, "@Id_Usuario", DbType.Int16, cheque.Id_Usuario);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DAChequesUpdateEstado
        /// <summary>
        /// Actualiza Cta.Cte de una Venta en base a Id_Compra
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cheques"></param>
        public void DAChequesUpdateEstado(string Connection, Cheques cheque)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Cheques_UpdateEstado");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Ventas", DbType.Int64, cheque.Id_Ventas);
                    DataBase.AddInParameter(cmd, "@Item", DbType.Int16, cheque.Item);
                    DataBase.AddInParameter(cmd, "@IdEstado", DbType.Int16, cheque.IdEstado);
                    DataBase.AddInParameter(cmd, "@Estado", DbType.String, cheque.Estado);
                    DataBase.AddInParameter(cmd, "@FechaEstado", DbType.DateTime, cheque.FechaEstado);
                    DataBase.AddInParameter(cmd, "@Id_Usuario", DbType.Int16, cheque.Id_Usuario);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DAChequesGetAllByVenta
        /// <summary>
        /// Devuelve lista de Cheques de una Venta
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cheques"></param>
        /// <returns><List>Cheques</List></returns>
        public List<Cheques> DAChequesGetAllByVenta(string Connection, Cheques cheque)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Cheques_GetAllByVenta");

                List<Cheques> listaCheques = new List<Cheques>();
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Ventas", DbType.Int64, cheque.Id_Ventas);

                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Cheques oCheques = new Cheques();

                        oCheques.Id_Ventas = Convert.ToInt64(Odbreader["Id_Ventas"]);
                        oCheques.Item = (int)(Odbreader["Item"]);
                        oCheques.Sucursal = (int)(Odbreader["Sucursal"]);
                        oCheques.CodBanco = Convert.ToInt16(Odbreader["CodBanco"]);
                        oCheques.Banco = (string)Odbreader["Banco"];
                        oCheques.Numero = (string)Odbreader["Numero"];
                        oCheques.FechaCh = (DateTime)Odbreader["FechaCh"];
                        oCheques.Monto = (Decimal)Odbreader["Monto"];
                        oCheques.IdEstado = (int)(Odbreader["IdEstado"]);
                        oCheques.Estado = (string)Odbreader["Estado"];
                        oCheques.FechaEstado = (DateTime)Odbreader["FechaEstado"];
                        oCheques.Id_Usuario = (int)(Odbreader["Id_Usuario"]);
                        if (oCheques.IdEstado == 1 || oCheques.IdEstado == 3)
                        {
                            TimeSpan tpDias = new TimeSpan();
                            tpDias = DateTime.Now - oCheques.FechaCh;
                            oCheques.Dias = tpDias.Days.ToString();
                        }

                        listaCheques.Add(oCheques);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listaCheques;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DAChequesGetAllByEstado
        /// <summary>
        /// Devuelve lista de Cheques por Estado en un rango de fechas
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cheques"></param>
        /// <returns><List>Cheques</List></returns>
        public List<Cheques> DAChequesGetAllByEstado(string Connection, Cheques chequeD, Cheques chequeH)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Cheques_GetAllByEstado");

                List<Cheques> listaCheques = new List<Cheques>();
                using (cmd)
                {
                    if (chequeD.IdEstado == 0)
                    {
                        DataBase.AddInParameter(cmd, "@IdEstado", DbType.Int16, null);
                    }
                    else
                    {
                        DataBase.AddInParameter(cmd, "@IdEstado", DbType.Int16, chequeD.IdEstado);
                    }
                    DataBase.AddInParameter(cmd, "@FechaD", DbType.DateTime, chequeD.FechaCh);
                    DataBase.AddInParameter(cmd, "@FechaH", DbType.DateTime, chequeH.FechaCh);

                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Cheques oCheques = new Cheques();

                        oCheques.Id_Ventas = Convert.ToInt64(Odbreader["Id_Ventas"]);
                        oCheques.Item = (int)(Odbreader["Item"]);
                        oCheques.Sucursal = (int)(Odbreader["Sucursal"]);
                        oCheques.CodBanco = Convert.ToInt16(Odbreader["CodBanco"]);
                        oCheques.Banco = (string)Odbreader["Banco"];
                        oCheques.Numero = (string)Odbreader["Numero"];
                        oCheques.FechaCh = (DateTime)Odbreader["FechaCh"];
                        oCheques.Monto = (Decimal)Odbreader["Monto"];
                        oCheques.IdEstado = (int)(Odbreader["IdEstado"]);
                        oCheques.Estado = (string)Odbreader["Estado"];
                        oCheques.FechaEstado = (DateTime)Odbreader["FechaEstado"];
                        oCheques.Id_Usuario = (int)(Odbreader["Id_Usuario"]);
                        if (oCheques.IdEstado == 1 || oCheques.IdEstado == 3)
                        {
                            TimeSpan tpDias = new TimeSpan();
                            tpDias = DateTime.Now - oCheques.FechaCh;
                            oCheques.Dias = tpDias.Days.ToString();
                        }
                        oCheques.IdCheque = oCheques.CodBanco.ToString();
                        string sTemp = oCheques.Item.ToString();
                        if (sTemp.Length < 2)
                        {
                            sTemp = "0" + oCheques.Item.ToString();
                        }
                        oCheques.IdCheque += sTemp;
                        oCheques.SFechaCh = oCheques.FechaCh.ToShortDateString();
                        oCheques.SFechaEstado = oCheques.FechaEstado.ToShortDateString();
                           

                        listaCheques.Add(oCheques);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listaCheques;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DAChequesGetOne
        /// <summary>
        /// Devuelve un Cheque
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cheques"></param>
        /// <returns>Cheques</returns>
        public Cheques DAChequesGetOne(string Connection, Cheques cheque)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Cheques_GetOne");

                Cheques oCheques = new Cheques();
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@CodBanco", DbType.Int16, cheque.CodBanco);
                    DataBase.AddInParameter(cmd, "@Numero", DbType.String, cheque.Numero);

                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        
                        oCheques.Id_Ventas = Convert.ToInt64(Odbreader["Id_Ventas"]);
                        oCheques.Item = (int)(Odbreader["Item"]);
                        oCheques.Sucursal = (int)(Odbreader["Sucursal"]);
                        oCheques.CodBanco = Convert.ToInt16(Odbreader["CodBanco"]);
                        oCheques.Banco = (string)Odbreader["Banco"];
                        oCheques.Numero = (string)Odbreader["Numero"];
                        oCheques.FechaCh = (DateTime)Odbreader["FechaCh"];
                        oCheques.Monto = (Decimal)Odbreader["Monto"];
                        oCheques.IdEstado = (int)(Odbreader["IdEstado"]);
                        oCheques.Estado = (string)Odbreader["Estado"];
                        oCheques.FechaEstado = (DateTime)Odbreader["FechaEstado"];
                        oCheques.Id_Usuario = (int)(Odbreader["Id_Usuario"]);
                        if (oCheques.IdEstado == 1 || oCheques.IdEstado == 3)
                        {
                            TimeSpan tpDias = new TimeSpan();
                            tpDias = DateTime.Now - oCheques.FechaCh;
                            oCheques.Dias = tpDias.Days.ToString();
                        }

                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return oCheques;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DAChequesDelete
        /// <summary>
        /// Borra todos los Cheques en la tabla Cheques de una Venta
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Cheques"></param>
        public void DAChequesDelete(string Connection, Cheques cheque)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Cheques_Delete");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Ventas", DbType.Int64, cheque.Id_Ventas);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region RptCheques
        public List<Cheques> RptCheques(List<Cheques> listaCheques)
        {
            try
            {
                return listaCheques;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion
    }
}
